package com.lw.nio.readFile;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;

/**
 * @Author LW
 * @2021/12/29 11:09
 */
public class ReadFileDemo {
    public static void main(String[] args) {
        Path path = Paths.get("/data/logs/web.log");
//属性
//获取路径中的文件名或者最后一个节点元素
        System.out.printf("FileName:%s%n", path.getFileName());
//路径节点元素的格式
        System.out.printf("NameCount:%s%n", path.getNameCount());
//遍历路径节点方法1
        Iterator<Path> names = path.iterator();
        int i = 0;
        while (names.hasNext()) {
            Path name = names.next();
            System.out.printf("Name %s:%s%n",i,name.toString());
            i++;
        }
//方法2
        for(int j = 0; j < path.getNameCount(); j++) {
            System.out.printf("Name %s:%s%n",j,path.getName(j));
        }
//父路径
        System.out.printf("Parent:%s%n",path.getParent());
//跟路径,比如"/"、"C:";如果是相对路径，则返回null。
        System.out.printf("Root:%s%n",path.getRoot());
//子路径，结果中不包含root，前开后闭
        System.out.printf("Subpath[0,2]:%s%n",path.subpath(0,2));

    }
}
